class Graph{
    constructor(isDirective=false){ 
        this.isDirective = isDirective; //默认是无向图
        this.verteies = []; //顶点的集合
        this.edges = new Map();  //边的集合  map类似对象的数据结构  键不局限于字符串
    }

    //添加顶点
    addVertex(v){
        if(!this.verteies.includes(v)){
            this.verteies.push(v);
            this.edges.set(v,[]);
        }
    }

    //添加边  v-->w
    addEdge(v,w){
        if(!this.edges.get(v)){
            this.addVertex(v);
        }

        if(!this.edges.get(w)){
            this.addVertex(w);
        }

        this.edges.get(v).push(w); //添加一个边
        if(!this.isDirective){
            //如果是无向图  w--->v
            this.edges.get(w).push(v);
        }
    }
}

let g = new Graph(true);

g.addVertex(0);

g.addVertex(1);

g.addVertex(2);

g.addVertex(3);

g.addVertex(4);

g.addVertex(5);

g.addEdge(0,5);

g.addEdge(0,1);

g.addEdge(2,0);

g.addEdge(2,3);

console.log(g)